import { useReactive } from "ahooks";
import { useModel } from "umi";
import { useEffect } from "react";
import compress from "gql-compress";

import personFemale from "../../assets/images/person_female.png";
import personMale from "../../assets/images/person_male.png";

const useInstitutionsByType = (oiType) => {
    const { graphqlFetch } = useModel("global");
    const { kuboUrl } = useModel("kubo");

    const state = useReactive({
        loading: false,
        institutions: [],
    });

    const loadInstitutionsByType = async () => {
        if (!oiType) return;
        state.loading = true;
        let res = await graphqlFetch({
            query,
            variables: {
                oiType,
            },
        });
        let institutions =
            res?.a?.nodes.map((item, i) => {
                return {
                    ...item,
                    id: "id-" + i,
                    value: {
                        name: item.oiName,
                        title: item.maInfo.osName,
                        type: oiType,
                    },
                    children: item.children.nodes.map((_item, j) => ({
                        reUuid: _item.reUuid,
                        ..._item.staff,
                        ..._item.position,
                        id: "id-" + i + "-" + j,
                        value: {
                            name: _item.position.opName,
                            title: _item.staff.osName,
                            type: "成员",
                            icon: _item.member?.omAvta ? kuboUrl + _item.member.omAvta : _item.member.omRoge === "男" ? personMale : personFemale,
                        },
                    })),
                };
            }) || [];
        state.institutions = institutions;
        state.loading = false;
        return institutions;
    };

    useEffect(() => {
        oiType && loadInstitutionsByType();
    }, [oiType]);

    return { ...state, loadInstitutionsByType };
};

const query = compress(`query MyQuery($oiType: String!) {
    a: allOrganizationInstitutions(condition: {oiType: $oiType, oiRoot: null}, orderBy: PY_NAME_ASC) {
        nodes {
            asInfo
            crInfo
            maInfo
            mpInfo
            oiCity
            oiComm
            oiDesc
            oiDist
            oiEsta
            oiLeve
            oiName
            oiPaid
            oiPhon
            oiPrin
            oiRoot
            oiStat
            oiStre
            oiType
            oiUuid
            oiZoja
            otInfo
            ptInfo
            pyName
            upInfo
            oiAddr
            ptInfo
            children: organizationInstitutionsMembersByOiUuid {
                totalCount
                nodes {
                    reUuid
                    staff: organizationStaffByOsUuid {
                        osName
                        osUuid
                        omUuid
                    }
                    position: organizationPositionByOpUuid {
                        opName
                        opUuid
                    }
                    member: organizationMemberByOmUuid {
                        omAvta
                        omRoge
                    }
                }
            }
        }
    }
}`);

export default useInstitutionsByType;
